package com.hui.servlet;

import com.hui.entity.Menu;
import com.hui.entity.Role;
import com.hui.service.MenuService;
import com.hui.service.RoleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description Role 控制层
 */
@WebServlet(urlPatterns = {"/findAllRole", "/delRole", "/updateRole", "/toUpdatePage", "/toAddRolePage", "/addRole"})
public class RoleServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String uri = req.getRequestURI();
        if (uri.endsWith("findAllRole")) {
            findAllRole(req, resp);
        } else if (uri.endsWith("delRole")) {
            delRole(req, resp);
        } else if (uri.endsWith("updateRole")) {
            updateRole(req, resp);
        } else if (uri.endsWith("toUpdatePage")) {
            toUpdatePage(req, resp);
        } else if (uri.endsWith("toAddRolePage")) {
            toAddRolePage(req, resp);
        } else if (uri.endsWith("addRole")) {
            addRole(req, resp);
        }

    }

    /**
     * 添加角色职位
     **/
    private void addRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String roleName = req.getParameter("roleName");
        String roleDesc = req.getParameter("roleDesc");
        String[] menuIds = req.getParameterValues("menuId");

        Role role = new Role(null, roleName, roleDesc);

        RoleService service = new RoleService();

        if (menuIds == null) {//当为空时,默认此管理员只有商品管理-全部商品
            String[] str = {"1", "14"};
            service.addRole(role, str);
            resp.sendRedirect("findAllRole");
            return;
        }

        service.addRole(role, menuIds);
        resp.sendRedirect("findAllRole");

    }

    /**
     * 跳转到新增职位界面
     **/
    private void toAddRolePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        MenuService service = new MenuService();
        List<Menu> list = service.findMenuAll();

        req.setAttribute("list", list);
        req.getRequestDispatcher("admin_role_add.jsp").forward(req, resp);
    }

    /**
     * 跳转到修改界面
     * 根据roleId查询职位和所管理的菜单
     **/
    private void toUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //接收参数
        int roleId = Integer.parseInt(req.getParameter("roleId"));
        System.out.println(roleId);

        RoleService service = new RoleService();

        Role role = service.findByRoleId(roleId);
        List<Menu> menuList = service.findMenuByRoleId(roleId);

        req.setAttribute("role", role);
        req.setAttribute("menuList", menuList);


        MenuService menuService = new MenuService();
        List<Menu> list = menuService.findMenuAll();
        req.setAttribute("list", list);

        req.getRequestDispatcher("admin_role_update.jsp").forward(req, resp);
    }

    /**
     * 修改职位操作
     **/
    private void updateRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        int roleId = Integer.parseInt(req.getParameter("roleId"));
        String roleName = req.getParameter("roleName");
        String roleDesc = req.getParameter("roleDesc");
        String[] menuIds = req.getParameterValues("menuId");

        Role role = new Role(roleId, roleName, roleDesc);
        RoleService service = new RoleService();

        service.updateRole(role, menuIds);

        resp.sendRedirect("findAllRole");

    }

    /**
     * 删除职位
     **/
    private void delRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        int roleId = Integer.parseInt(req.getParameter("roleId"));
        System.out.println(roleId);
        RoleService service = new RoleService();

        service.delRole(roleId);

    }

    /**
     * 获取所有职位信息
     **/
    private void findAllRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoleService service = new RoleService();
        List<Role> list = service.getAllRole();

        req.setAttribute("list", list);

        req.getRequestDispatcher("admin_role_list.jsp").forward(req, resp);

    }
}
